package com.example.widget_lib.widget.timeruler;

import com.example.widget_lib.ResourceTable;
import com.example.widget_lib.utils.AttrUtils;
import com.example.widget_lib.utils.PxUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.global.resource.solidxml.TypedAttribute;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

import java.util.HashMap;
import java.util.Set;

/**
 * @author Amosnail
 * @date 2017/4/11
 * @desc 时间刻度缩放选择
 */
public class TimeRulerPickScaleView extends Component implements Component.DrawTask, Component.TouchEventListener {
    private PixelMap mBitmap;
    //    private boolean isDownRight = true;
    private Paint mBitmapPaint;

    private double mMaxTotalTimePerCell;
    private double mMinTotalTimePerCell;
    private double mDegree;
    private double mCurrentTotalTimePerCell;
    private double mDownY;
    private double mTouchY;
    private double mBitmapPosition;//图片top位置
    private RectFloat mClickRecF;
    private double mWidth;
    private double mHeight;
    private TimeRulerPickViewListener timeRulerPickViewListener;
    private PixelMapHolder holder;

    public TimeRulerPickScaleView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initDefaultValue(attrSet);
        init();
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                measure();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
        setTouchEventListener(TimeRulerPickScaleView.this);
        addDrawTask(TimeRulerPickScaleView.this);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    private void initDefaultValue(AttrSet attrSet) {
        mMaxTotalTimePerCell = AttrUtils.getIntegerFromAttr(attrSet, "maxTotalTimePerCell", 0);
        mMinTotalTimePerCell = AttrUtils.getIntegerFromAttr(attrSet, "minTotalTimePerCell", 0);
    }

    private void init() {
        try {
            Resource resource = getResourceManager().getResource(ResourceTable.Media_timerulerview_move);
            mBitmap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mWidth = PxUtil.vp2px(24);
        mHeight = PxUtil.vp2px(144);
        mBitmapPaint = new Paint();

        holder = new PixelMapHolder(mBitmap);
    }

    private void measure() {
        if (mClickRecF == null) {
            mClickRecF = new RectFloat();
            int wid = mBitmap.getImageInfo().size.width;
            int hei = mBitmap.getImageInfo().size.height;
            mBitmapPosition = mHeight / 2.0F - hei / 2.0F;

            mClickRecF.left = 0;
            mClickRecF.right = wid;
            mClickRecF.top = (float) mBitmapPosition;
            mClickRecF.bottom = (float) (mBitmapPosition + hei);

            mDegree = (mMaxTotalTimePerCell - mMinTotalTimePerCell) / (mHeight - hei);
            mCurrentTotalTimePerCell = mMaxTotalTimePerCell - mDegree * mBitmapPosition;
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent event) {
        switch (event.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
//                mClickRecF.top = mBitmapPosition;
//                mClickRecF.bottom = mBitmapPosition + mBitmap.getImageInfo().size.height;
//                double px = event.getPointerPosition(event.getIndex()).getX();
//                double py = event.getPointerPosition(event.getIndex()).getY();
//                if (mClickRecF.isInclude(px, py)) {
//                    isDownRight = true;
//                }
                mDownY = event.getPointerPosition(event.getIndex()).getY();
                break;
            case TouchEvent.POINT_MOVE:
//                if (isDownRight) {
                mTouchY = event.getPointerPosition(event.getIndex()).getY();
                double diff = mTouchY - mDownY;
                if (diff > 100) {
                    invalidate();
                    mDownY = mTouchY;
                    return true;
                }
                mBitmapPosition += diff;
                if (mBitmapPosition < 0) {
                    mBitmapPosition = 0;
                } else if (mBitmapPosition > (mHeight - mBitmap.getImageInfo().size.height)) {
                    mBitmapPosition = mHeight - mBitmap.getImageInfo().size.height;
                }
                mCurrentTotalTimePerCell = mMaxTotalTimePerCell - mDegree * mBitmapPosition;
                if (timeRulerPickViewListener != null) {
                    timeRulerPickViewListener.onTotalTimePerCellChanged((int) mCurrentTotalTimePerCell);
                }
                invalidate();
                mDownY = mTouchY;
//                }
                break;
            case TouchEvent.PRIMARY_POINT_UP:
            case TouchEvent.CANCEL:
//                isDownRight = true;
                //传递最后的值
                break;
            default:
                return false;
        }
        return true;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.drawPixelMapHolder(holder,
                (float) (mWidth / 2.0F - mBitmap.getImageInfo().size.width / 2.0F), (float) mBitmapPosition,
                mBitmapPaint);
    }

    public void setTimeRulerPickViewListener(TimeRulerPickViewListener timeRulerPickViewListener) {
        this.timeRulerPickViewListener = timeRulerPickViewListener;
    }

    public interface TimeRulerPickViewListener {
        void onTotalTimePerCellChanged(int totalTimePerCell);
    }
}
